home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / MacPowerオリジナル / キョービのプログラマー / Flash Trash src / ShowInitIcon.c < prev   
C/C++ Source or Header  |  1997-09-06  |  3KB  |  117 lines

  1. #include <Gestalt.h>
  2. #include <Icons.h>
  3. #include <OSUtils.h>
  4.  
  5. #ifndef    CurApName
  6. #define    CurApName    0x0910
  7. #endif
  8. extern     short        my_h : CurApName+32-4;
  9. extern    short        my_check : CurApName+32-2;
  10.  
  11. #ifndef NULL
  12. #define NULL ( (void *)0 )
  13. #endif
  14.  
  15. #define ICON_HEIGHT        32
  16. #define ICON_WIDTH        32
  17. #define    FIRST_X            8
  18. #define    BOTTOM_MARGIN    8
  19. #define DEF_MOVE_X_BY    40
  20. #define    CHECKSUM_CONST    0x1021
  21. #define MIN_BIT_DEPTH    4
  22.  
  23. OSErr ShowInitIcon( short icon_num, short move_x_by);
  24. void plot_icn(Rect *r, Handle icn_handle);
  25.  
  26. OSErr ShowInitIcon( short icon_num, short move_x_by)
  27. {
  28. //    short        err;
  29.     Handle        icon_h=NULL;        /* handle to 'ICN#' or 'cicn' resource */
  30.     Boolean        in_color=FALSE;        /* TRUE='cicn' resource, else 'ICN#' */
  31.     GrafPtr        oldport;
  32.     GrafPort    newport;            /* Entire GrafPort structure */
  33.     Rect         r;                    /* where to put the icon */
  34. //    SysEnvRec    sys_rec;
  35.     short        screen_width;
  36.     long        resp;
  37.  
  38.     if(    (Gestalt(gestaltQuickdrawFeatures, &resp) == noErr) && (resp & (31 - gestaltHasColor)) ) {
  39. //    if ( (err = SysEnvirons(1, &sys_rec)) != 0 )
  40. //        return(err);
  41. //    if ( sys_rec.hasColorQD )        /* a Mac II or later... */
  42. //    {
  43.         GDHandle gdev = GetGDevice();    /* get main screen */
  44.         if ( (*(*gdev)->gdPMap)->pixelSize >= MIN_BIT_DEPTH )
  45.             icon_h = (Handle)GetCIcon(icon_num);
  46.         if ( icon_h != NULL )
  47.             in_color = TRUE;
  48.     }
  49.     
  50.     if ( icon_h == NULL )        /* no icon yet, try a B&W one */
  51.         icon_h = Get1Resource('ICN#', icon_num);
  52.     if ( icon_h == NULL )
  53.         return(resNotFound);    /* we're SOL here */
  54.     HNoPurge( icon_h );
  55.  
  56.     GetPort(&oldport);        /* save old GrafPort for later restoring */
  57.  
  58.     OpenPort(&newport);
  59.     SetPort(&newport);
  60.     
  61.     if ( ((my_h <<1) ^ CHECKSUM_CONST) != my_check )
  62.         my_h = FIRST_X;
  63.  
  64.     screen_width = newport.portRect.right - newport.portRect.left;
  65.     screen_width -= screen_width % DEF_MOVE_X_BY;     /* in case screen isn't multiple of 40 */
  66.     r.left = my_h % screen_width;
  67.     r.right = r.left + ICON_WIDTH;
  68.     r.top = newport.portRect.bottom - ((BOTTOM_MARGIN+ICON_HEIGHT) * (1 + my_h / screen_width));
  69.     r.bottom = r.top + ICON_HEIGHT;
  70.     
  71.     if ( in_color )
  72.         PlotCIcon(&r, (CIconHandle)icon_h);        /* Toolbox's plot routine */
  73.         
  74.     else
  75.         plot_icn(&r, icon_h);                    /* our routine */
  76.     
  77.     if ( move_x_by == -1 )
  78.         move_x_by = DEF_MOVE_X_BY;
  79.  
  80.     my_h += move_x_by;
  81.     my_check = (my_h<<1) ^ CHECKSUM_CONST;
  82.  
  83.     if ( in_color )
  84.         DisposeCIcon((CIconHandle)icon_h);
  85.     else
  86.         ReleaseResource(icon_h);
  87.     SetPort(oldport);
  88.     ClosePort(&newport);
  89.     return(0);
  90. }
  91.  
  92. void plot_icn(r, icn_handle)
  93.     Rect    *r;
  94.     Handle    icn_handle;
  95. {
  96.     BitMap        bm;
  97.     char        flags;
  98.     GrafPtr        cur_port;
  99.     
  100.     flags = HGetState(icn_handle);
  101.     HLock(icn_handle);
  102.     GetPort(&cur_port);
  103.     
  104.     bm.baseAddr = *icn_handle + ICON_WIDTH*ICON_HEIGHT/8;    /* points to mask */
  105.     bm.rowBytes = ICON_WIDTH / 8;            /* ICON_WIDTH/8 must be power of 2 */
  106.     SetRect(&bm.bounds, 0, 0, ICON_WIDTH, ICON_HEIGHT);
  107.  
  108.         /* punch a hole in the Desktop using bit-clear mode and the mask */
  109.     CopyBits(&bm, &cur_port->portBits, &bm.bounds, r, srcBic, NULL);
  110.  
  111.         /* now copy the icon into the punched hole */
  112.     bm.baseAddr = *icn_handle;        /* points to ICON */
  113.     CopyBits(&bm, &cur_port->portBits, &bm.bounds, r, srcOr, NULL);
  114.     
  115.     HSetState(icn_handle, flags);
  116. }
  117.